<!DOCTYPE html>
<html>
<head>
    <include file="Include:meta"/>
    <title>添加项目款项-目的地运营集团数据管理平台</title>
    <include file="Include:style"/>
    <style>
        .w50 {
            width: 50px;
        }
    </style>
</head>
<body>
<include file="Include:header"/>
<include file="Include:left"/>
<div id="main">
    <div class="crumbs">
        <div class="crumbs-text">项目合同列表 > 添加项目合同</div>
    </div>
    <div id="content">
        <form action="__APP__/Contract/add" method="post" id="project_add_form" onsubmit="return verify();">
            <div class="form-main">
                <div class="form-title">添加项目合同</div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>所属中心</div>
                    <div class="input-item">
                        <select name="center_id" class="form-control w100 required_select" autocomplete="off">
                            <option value="">全部</option>
                            <option value="130004">建设管理</option>
                            <option value="130005">运营管理</option>
                        </select>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>项目编号-名称</div>
                    <div class="input-item">
                        <select name="project_id" class="form-control w400 required_select" autocomplete="off">
                            <option value="">请选择</option>
                            <foreach name="res_project" item="vo">
                                <option value="{$vo.project_id}">{$vo.project_num}-{$vo.name}</option>
                            </foreach>
                        </select>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>合同编号</div>
                    <div class="input-item">
                        <input type="text" class="form-control w100" name="number" value="{$number}" autocomplete="off"
                               readonly>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>合同金额</div>
                    <div class="input-item">
                        <input type="text" class="form-control w100" name="contract_money" autocomplete="off">
                        <span class="error-item">必填项</span>
                        <span class="error-item">必须为正整数</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>签订日期</div>
                    <div class="input-item">
                        <input type="text" name="sign_contract_date" class="form-control Wdate w100 required_text"
                               onClick="WdatePicker()" value="{$today}" readonly autocomplete="off" required>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>合同日期</div>
                    <div class="input-item">
                        <input type="text" name="sign_start_date" class="form-control Wdate w100 required_text"
                               onClick="WdatePicker()" autocomplete="off" readonly>--<span class="error-item">必填项</span>
                        <input type="text" name="sign_end_date" class="form-control Wdate w100 required_text"
                               onClick="WdatePicker()" autocomplete="off" readonly><span
                            class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>服务期数(月)</div>
                    <div class="input-item">
                        <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'')" class="form-control w100"
                               name="serve_mouth" autocomplete="off" placeholder="只填数字">
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>签订人</div>
                    <div class="input-item">
                        <select name="sign_user_id" class="form-control w100 required_select" autocomplete="off"
                                required>
                            <option value="{:$_SESSION['dms_uid']}">{:$_SESSION['dms_username']}</option>
                            <foreach name="user_list" item="vo">
                                <option value="{$vo.id}">{$vo.username}</option>
                            </foreach>
                        </select>
                        <br>
                        <span style="color: grey">（目标PK、绩效PK等数据统计时，合同金额绩效归属于“签订人”。）</span>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>合同状态</div>
                    <div class="input-item">
                        <select name="status" class="form-control w100 required_select" autocomplete="off">
                            <option value="">请选择</option>
                            <for start="1" end="4">
                                <option value="{$i}">{:get_contract_status($i)}</option>
                            </for>
                        </select>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">付款方式</div>
                    <div class="input-item">
                        <select name="pay_way" class="form-control w100 required_select" autocomplete="off">
                            <option value="网银转账">网银转账</option>
                            <option value="现金">现金</option>
                            <option value="支票">支票</option>
                            <option value="承诺汇票">承诺汇票</option>
                        </select>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">合同附件</div>
                    <div class="input-item">
                        <div class="input-group w250 left">
                            <input type="text" name="file_name" class="form-control file-name" placeholder="请上传附件"
                                   readonly autocomplete="off">
                            <span class="input-group-btn">
									<button class="btn file-submit" type="button">上传</button>
								</span>
                        </div>
                        <button class="btn file-delete ml5 hide" type="button">删除</button>
                        <input type="hidden" name="file_url" class="file-url" autocomplete="off">
                        <span class="error-item">请上传文件</span>
                        <span class="error-item">文件名必填</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>款项阶段</div>
                    <div class="input-item">
                        <select name="stages" class="form-control w100 required_select" autocomplete="off">
                            <for start="1" end="20" comparison="elt" step="1">
                                <option value="{$i}">{$i}</option>
                            </for>
                        </select>
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-sub-title">款项阶段<span style="color: grey">（该功能只设置基础管理费等固定收入，考核类等浮动收入在客户考评模块中设置）</span>
                </div>
                <div class="form-table-item">
                    <table class="table stage_table">
                        <tr class="form-table-header">
                            <td>阶段</td>
                            <td><span class="asterisk">*</span>应收款金额</td>
                            <td><span class="asterisk">*</span>应收款日期</td>
                            <!--<td><span class="asterisk">*</span>应开票金额</td>-->
                            <!--<td><span class="asterisk">*</span>应开票日期</td>-->
                            <td style="display: none"><span class="asterisk">*</span>收款状态</td>
                            <td><span class="asterisk">*</span>类型</td>
                        </tr>
                        <for start="1" end="20" comparison="elt" step="1">
                            <tr
                            <if condition="$i eq 1">class="cur tr"
                                <else/>
                                class="hide tr"
                            </if>
                            >
                            <td>└第{$i}阶段</td>
                            <td>
                                <input type="text" name="stage_money[]" class="form-control td_text w100 money_in"
                                       autocomplete="off" onclick="verify()">
                                <span class="error-item">必填项</span><span class="error-item">必须为大于0的正数</span>
                            </td>
                            <td>
                                <input type="text" name="stage_date[]" class="form-control Wdate w100 td_text"
                                       onClick="WdatePicker()" autocomplete="off" onclick="verify()">
                                <span class="error-item">必填项</span>
                            </td>
                            <!--<td>
                                <input type="text" name="ticket_money[]" class="form-control td_text w100 money_all"
                                       autocomplete="off">
                                <span class="error-item">必填项</span><span class="error-item">必须为大于0的正数</span>
                            </td>-->
                            <!--<td>
                                <input type="text" name="ticket_date[]" class="form-control Wdate w100 td_text"
                                       onClick="WdatePicker({onpicked:dd})" readonly autocomplete="off">
                                <span class="error-item">必填项</span>
                            </td>-->
                            <td style="display: none">
                                <select name="back_status[]" class="form-control w100 required_select"
                                        autocomplete="off">
                                    <option value="2">未收</option>
                                </select>
                                <span class="error-item">必填项</span>
                            </td>
                            <td>
                                <select name="type[]" class="form-control w100 required_select" autocomplete="off">
                                    <option value="1">常规</option>
                                    <option value="2">预收</option>
                                    <option value="3">尾款</option>
                                    <option value="4">保证金</option>
                                </select>
                                <span class="error-item">必填项</span>
                            </td>
                            </tr>
                        </for>
                        <tr>
                            <td>合计</td>
                            <td class="sk_all"></td>
                            <td></td>
                            <td class="kp_all"></td>
                        </tr>
                    </table>
                </div>
            </div>
            <p class="red Text_c bili_item hide"></p>
            <div class="form-btn-item"><input type="submit" value="提交" class="btn btn-submit">
                <button class="btn btn-default goback" type="button">返回</button>
            </div>
        </form>
    </div>
</div>
</body>
<include file="Include:javascript"/>
<script type="text/javascript">
    function verify() {
        var center_id = $("select[name='center_id']").val()
        if(center_id.length==0){
            defeated_alert('请选择所属中心');
            return false;
        }
        var project_id = $("select[name='project_id']").val()
        if(project_id.length==0){
            defeated_alert('请选择项目编号-名称');
            return false;
        }
        var contract_money = $("input[name='contract_money']").val()
        if(contract_money.length==0){
            defeated_alert('请填写合同金额');
            return false;
        }
        var sign_start_date = $("input[name='sign_start_date']").val()
        if(sign_start_date.length==0){
            defeated_alert('请填写合同开始日期');
            return false;
        }
        var sign_end_date = $("input[name='sign_end_date']").val()
        if(sign_end_date.length==0){
            defeated_alert('请填写合同结束日期');
            return false;
        }
        var serve_mouth = $("input[name='serve_mouth']").val()
        if(serve_mouth.length==0){
            defeated_alert('请填写服务期数');
            return false;
        }
        var status = $("select[name='status']").val()
        if(status.length==0){
            defeated_alert('请填写合同状态');
            return false;
        }
    }
    $(function () {
        KindEditor.ready(function (K) {
            //上传附件
            var editorFile = K.editor();
            $('.file-submit').on('click', function () {
                var parents = $(this).parents(".input-group");
                editorFile.loadPlugin('insertfile', function () {
                    document.cookie = "file_name=contract";
                    document.cookie = "file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
                    editorFile.plugin.fileDialog({
                        clickFn: function (url, title) {
                            parents.nextAll(".error-item").hide();
                            parents.find(".file-name").val(title).prop("readonly", false);
                            parents.nextAll(".file-url").val(url);
                            parents.nextAll(".file-delete").show();
                            editorFile.hideDialog();
                        }
                    });
                });
            });
        });

        //删除附件
        $(".file-delete").click(function () {
            $(this).hide();
            $(this).nextAll(".error-item").hide();
            $(this).nextAll(".file-url").val("");
            $(this).prevAll(".input-group").find(".file-name").val("").prop("readonly", true);
        });
        /*阶段联动*/
        $("[name='stages']").change(function () {
            var val = $(this).val();
            $('.stage_table tr').each(function (i) {
                if (val > i) {
                    $('.stage_table .tr').eq(i).show().addClass('cur');
                } else {
                    $('.stage_table .tr').eq(i).hide().removeClass('cur').find('.form-control').val('');
                }
            })
            var sk = 0;
            $(".cur .money_in").each(function () {
                var val = $(this).val();
                sk += +val;
            })
            if (sk == '0') {
                $('.sk_all').html('');
            } else {
                $('.sk_all').html(sk);
            }
            var kp = 0;
            $(".cur .money_all").each(function () {
                var val = $(this).val();
                kp += +val;
            })
            if (kp == '0') {
                $('.kp_all').html('');
            } else {
                $('.kp_all').html(kp);
            }
            var sk = $('.sk_all').html(),
                all = $("[name='contract_money']").val(),
                ts = sk / all * 100,
                last = ts.toFixed(2) - 0;
            if (all != '') {
                if (sk) {
                    $('.bili_all').html("" + last + "%");
                }
            }
        })
        /*$("[name='center_id']").change(function () {
            var val = $(this).val();
            $.ajax({
                type: "POST",
                async: false,
                dataType: "json",
                data: {"center_id": val, "action": "project"},
                url: "__APP__/Financial/project_funds_ajax",
                success: function (data) {
                    if (data) {
                        var html = "<option value='-10'>请选择</option>";
                        for (var i = 0; i < data.length; i++) {
                            html += "<option value='" + data[i].id + "'>[" + data[i].project_num + "]" + data[i].project_name + "</option>"
                        }
                        $("[name='project_id']").html(html).select2();
                    }
                }
            })
        })*/
        /*百分比换算*/
        $('body').on('blur', '.cur .money_in', function () {
            var val = $(this).val(),
                all = $("[name='contract_money']").val(),
                end = val / all * 100,
                end2 = end.toFixed(2) - 0;
            $(this).parents('tr').find(".money_all").val(val);
            var sk = 0;
            $(".cur .money_in").each(function () {
                var val = $(this).val();
                sk += +val;
            })
            if (sk == '0') {
                $('.sk_all').html('');
            } else {
                $('.sk_all').html(sk);
            }
            var kp = 0;
            $(".cur .money_all").each(function () {
                var val = $(this).val();
                kp += +val;
            })
            if (kp == '0') {
                $('.kp_all').html('');
            } else {
                $('.kp_all').html(kp);
            }
            if (all != '') {
                if (val != '') {
                    $(this).parents('tr').find("[name='bili[]']").val(end2);
                    $(this).parents('tr').find('.bili').html("<i>" + end2 + "</i>%");
                    var sk = $('.sk_all').html(),
                        ts = sk / all * 100,
                        last = ts.toFixed(2) - 0;
                    if (sk) {
                        $('.bili_all').html("" + last + "%");
                    }
                }
            }
        })
        $("[name='contract_money']").blur(function () {
            var val = $(this).val();
            if (val == '') {
                $('.bili').html('');
                $('.bili_all').html('');
                $("[name='bili[]']").val('');
            } else {
                $(".cur .money_in").each(function () {
                    var all = $(this).val(),
                        end = all / val * 100,
                        end2 = end.toFixed(2) - 0;
                    if (all != '') {
                        $(this).parents('tr').find('.bili').html("<i>" + end2 + "</i>%");
                        $(this).parents('tr').find("[name='bili[]']").val(end2);
                    }
                })
                var sk = $('.sk_all').html(),
                    ts = sk / val * 100,
                    last = ts.toFixed(2) - 0;
                if (sk) {
                    $('.bili_all').html("" + last + "%");
                }
            }

        })
        $("body").on('blur', '.cur .money_all', function () {
            var kp = 0;
            $(".cur .money_all").each(function () {
                var val = $(this).val();
                kp += +val;
            })
            $('.kp_all').html(kp);
        })

        /*表单验证*/
        $(".btn-submit").click(function () {
            var tag = false;

            $('tr.cur').each(function () {
                var kpval = ($(this).find("[name='ticket_date[]']").val().replace(/-/, '')).replace(/-/, ''),
                    yjval = ($(this).find("[name='warn_date[]']").val().replace(/-/, '')).replace(/-/, ''),
                    ckval = ($(this).find("[name='press_date[]']").val().replace(/-/, '')).replace(/-/, '');
                if (yjval > ckval) {
                    $(this).find("[name='press_date[]']").addClass('red_border');
                    $('.bili_item').html("预警日期不能大于催款日期").show();
                    tag = true;
                }
                if (kpval > yjval) {
                    $(this).find("[name='warn_date[]']").addClass('red_border');
                    $('.bili_item').html("应开票日期不能大于预警日期").show();
                    tag = true;
                }
            })

            $("[name='money']").each(function () {
                var val = $(this).val();
                if (val != '' && !/^[1-9]\d*$/.test(val)) {
                    $(this).nextAll('.error-item').eq(1).show();
                    tag = true;
                }
            })
            $("[name='contract_money']").each(function () {
                var val = $(this).val();
                if (val != '' && !/^[0-9]+$/.test(val)) {
                    $(this).nextAll('.error-item').eq(1).show();
                    tag = true;
                }
            })

            var kp = 0,
                moneyall = $("[name='contract_money']").val(),
                tuse = true;
            $(".cur .money_all").each(function () {
                var val = $(this).val();
                kp += +val;
                if (val != '' && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(val)) {
                    $(this).nextAll('.error-item').eq(1).show();
                    tag = true;
                }
                if (val == '') {
                    tuse = false;
                }
            })

            var sk = 0,
                tuee = true;
            $(".cur .money_in").each(function () {
                var val = $(this).val();
                sk += +val;
                if (val != '' && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(val)) {
                    $(this).nextAll('.error-item').eq(1).show();
                    tag = true;
                }
                if (val == '') {
                    tuee = false;
                }
            })
            if (moneyall != '') {
                if (tuse == true && kp != moneyall) {
                    $('.bili_item').html("应开票金额合计应等于合同金额").show();
                    tag = true;
                }
                if (sk != moneyall && tuee == true) {
                    $('.bili_item').html("应收款金额合计应等于合同金额").show();
                    tag = true;
                }
            } else {
                if (kp != sk) {
                    $('.bili_item').html("应开票金额合计应等于应收款金额合计").show();
                    tag = true;
                }
            }

            $('.cur .td_text').each(function () {
                var val = $(this).val();
                if (val == '') {
                    $(this).next('.error-item').show();
                    tag = true;
                }
            })
            $('.required_text').each(function () {
                var val = $(this).val();
                if (val == '') {
                    $(this).next('.error-item').show();
                    tag = true;
                }
            })
            $('.required_select').each(function () {
                var val = $(this).val();
                if (val == -10) {
                    $(this).siblings('.error-item').show();
                    tag = true;
                }
            })
            $(".file-name").each(function () {
                $val = $(this).val();
                if (($val == "" || /^\s+$/.test($val)) && !$(this).prop("readonly")) {
                    $(this).parents(".input-group").nextAll(".error-item").eq(1).show();
                    tag = true;
                }
            });
            if (tag) {
                return false;
            }
        });

        /*取消错误提示*/
        $('.required_select,.td_text').change(function () {
            $(this).siblings('.error-item').hide();
        })
        $(".required_text,.td_text,[name='contract_money']").focus(function () {
            $(this).nextAll('.error-item').hide();
        })
        $('.money_in').blur(function () {
            var val = $(this).val();
            if (val != '') {
                $('.money_all').siblings('.error-item').hide();
            }
        })
        $('.money_all,.money_in').focus(function () {
            $('.bili_item').hide();
        })
        $("[name='press_date[]'],[name='warn_date[]'],[name='ticket_date[]']").click(function () {
            $(this).removeClass('red_border');
            $('.bili_item').hide();
        })
        $("[name='ticket_date[]']").click(function () {
            $(this).parents('tr').find("[name='press_date[]']").removeClass('red_border');
            $(this).parents('tr').find("[name='warn_date[]']").removeClass('red_border');
        })
    });

    function dd() {
        var val = $(this).val(),
            chuo = new Date(val).getTime(),
            th = chuo + 15 * 24 * 60 * 60 * 1000,
            tr = chuo + 30 * 24 * 60 * 60 * 1000,
            las = gettime(th),
            lat = gettime(tr);
        $(this).parents('tr').find("[name='warn_date[]']").val(las).siblings('.error-item').hide();
        $(this).parents('tr').find("[name='press_date[]']").val(lat).siblings('.error-item').hide();
    }

    function gettime(t) {
        var _time = new Date(t);
        year = _time.getFullYear();
        month = _time.getMonth() + 1;
        date = _time.getDate();
        if (month < 10 && date < 10) {
            return year + "-0" + month + "-0" + date;
        } else if (month < 10 && date >= 10) {
            return year + "-0" + month + "-" + date;
        } else if (month >= 10 && date < 10) {
            return year + "-" + month + "-0" + date;
        } else if (month >= 10 && date >= 10) {
            return year + "-" + month + "-" + date;
        }

    }
</script>
</html>